package com.user_hosted_storage.server.dal;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

import com.user_hosted_storage.entities.Client;
import com.user_hosted_storage.entities.Community;
import com.user_hosted_storage.entities.CommunityMember;

@Stateless
@LocalBean
public class CommunityFacade extends GenericFacade<Community> {

	public List<CommunityMember> getLeastOccupiedHosts(double fileSize, long communityId, Client client) {
		
		Query query = em.createQuery("SELECT c FROM CommunityMember c where c.community.id = :id and c.usedSpace+:fileSize < c.sharedSize order by c.usedSpace ASC");
		
		query.setParameter("id", communityId);
		query.setParameter("fileSize", fileSize);
		query.setMaxResults(2); 
	
		@SuppressWarnings("unchecked")
		List<CommunityMember> resultList = query.getResultList();
		
		return resultList;
	}
}
